﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Threading;

namespace TS_XY_Train_Repaire.Main_Base.DigitalMap
{
    public partial class TS_Main_Base_Digital_Map_Map_Edit : DevExpress.XtraEditors.XtraForm
    {
        public int scrollspeed = 20;//放大缩小速率
        public int ScreenWidth, ScreenHeight;//PictureBox宽高
        public Double ScrollX, ScrollY;//地图XY坐标调整量
        public int MapWidth = 1024;//背景原图宽像素
        public int MapHeight = 768;//背景原图高像素
        public int ScreenVariableX = 0;//地图X像素偏移量
        public int ScreenVariableY = 0;//地图Y像素偏移量
        public int Mouse_M_S_X = 0;//鼠标按下X像素
        public int Mouse_M_S_Y = 0;//鼠标按下Y像素
        public int Mouse_M_E_X = 0;//鼠标释放X像素
        public int Mouse_M_E_Y = 0;//鼠标释放Y像素
        Bitmap _img = null;//原图
        Bitmap _img_b = null;//过渡图
        Bitmap _img_i = null;//插入图
        Bitmap _img_h = null;//高亮图
        public float _Pen_Wid = (float)2;
        public static string _WorkPlaceID = "-1";
        public static string _WorkPlaceName = "-1";
        public TS_Main_Base_Digital_Map_Map_Edit()
        {
            InitializeComponent();
        }


        private void TS_Main_Base_Digital_Map_Map_Edit_Load(object sender, EventArgs e)
        {
            Init_MapMain_TreeList();
            Init_MapElement_TreeList();
        }

        private void TS_Main_Base_Digital_Map_Map_Edit_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }

        private void pc_Center_Resize(object sender, EventArgs e)
        {
            pb_Main.Size = pc_Center.Size;
            pb_Main.Location = new Point(0, 0);
        }
        #region//--系统事件--//
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void bt_Debug_Click(object sender, EventArgs e)
        {
            Main_Base_DigitalMap_Info_ManaForm _uc = new Main_Base_DigitalMap_Info_ManaForm();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                Init_MapMain_TreeList();
            }
            else
            {
                Init_MapMain_TreeList();
            }
        }

        private void bt_Debug_1_Click(object sender, EventArgs e)
        {
            Main_Base_DigitalMap_Element_ManaForm _uc = new Main_Base_DigitalMap_Element_ManaForm();
            if (_uc.ShowDialog()==DialogResult.OK)
            {
                Init_MapElement_TreeList();
            }
            else
            {
                Init_MapElement_TreeList();
            }
        }
        #endregion
        #region//--功能区事件--//
        private void cb_Left_CheckedChanged(object sender, EventArgs e)
        {
            if ((cb_Left.Checked))
            {
                te_Map_Left.Enabled = true;
                bt_Map_Left_Add.Enabled = true;
                bt_Map_Left_Des.Enabled = true;
            }
            else
            {
                te_Map_Left.Enabled = false;
                bt_Map_Left_Add.Enabled = false;
                bt_Map_Left_Des.Enabled = false;
            }
        }

        private void bt_Map_Left_Add_Click(object sender, EventArgs e)
        {
            te_Map_Left.Text = (Convert.ToInt16(te_Map_Left.Text.Trim()) + 1).ToString();
        }

        private void bt_Map_Left_Des_Click(object sender, EventArgs e)
        {
            te_Map_Left.Text = (Convert.ToInt16(te_Map_Left.Text.Trim()) - 1).ToString();
        }

        private void cb_Right_CheckedChanged(object sender, EventArgs e)
        {
            if ((cb_Right.Checked))
            {
                te_Map_Right.Enabled = true;
                bt_Map_Right_Add.Enabled = true;
                bt_Map_Right_Des.Enabled = true;
            }
            else
            {
                te_Map_Right.Enabled = false;
                bt_Map_Right_Add.Enabled = false;
                bt_Map_Right_Des.Enabled = false;
            }
        }

        private void bt_Map_Right_Add_Click(object sender, EventArgs e)
        {
            te_Map_Right.Text = (Convert.ToInt16(te_Map_Right.Text.Trim()) + 1).ToString();
        }

        private void bt_Map_Right_Des_Click(object sender, EventArgs e)
        {
            te_Map_Right.Text = (Convert.ToInt16(te_Map_Right.Text.Trim()) - 1).ToString();
        }

        private void cb_Up_CheckedChanged(object sender, EventArgs e)
        {
            if ((cb_Up.Checked))
            {
                te_Map_Up.Enabled = true;
                bt_Map_Up_Add.Enabled = true;
                bt_Map_Up_Des.Enabled = true;
            }
            else
            {
                te_Map_Up.Enabled = false;
                bt_Map_Up_Add.Enabled = false;
                bt_Map_Up_Des.Enabled = false;
            }
        }

        private void bt_Map_Up_Add_Click(object sender, EventArgs e)
        {
            te_Map_Up.Text = (Convert.ToInt16(te_Map_Up.Text.Trim()) + 1).ToString();
        }

        private void bt_Map_Up_Des_Click(object sender, EventArgs e)
        {
            te_Map_Up.Text = (Convert.ToInt16(te_Map_Up.Text.Trim()) - 1).ToString();
        }

        private void cb_Down_CheckedChanged(object sender, EventArgs e)
        {
            if ((cb_Down.Checked))
            {
                te_Map_Down.Enabled = true;
                bt_Map_Down_Add.Enabled = true;
                bt_Map_Down_Des.Enabled = true;
            }
            else
            {
                te_Map_Down.Enabled = false;
                bt_Map_Down_Add.Enabled = false;
                bt_Map_Down_Des.Enabled = false;
            }
        }

        private void bt_Map_Down_Add_Click(object sender, EventArgs e)
        {
            te_Map_Down.Text = (Convert.ToInt16(te_Map_Down.Text.Trim()) + 1).ToString();
        }

        private void bt_Map_Down_Des_Click(object sender, EventArgs e)
        {
            te_Map_Down.Text = (Convert.ToInt16(te_Map_Down.Text.Trim()) - 1).ToString();
        }

        private void bt_Change_Map_Size_Click(object sender, EventArgs e)
        {
            int _iLeft = 0;
            int _iRight = 0;
            int _iUp= 0;
            int _iDown = 0;
            Boolean _HasMap = false;
            try
            {
                if (tl_Main.FocusedNode["f_id"].ToString().Length > 0)
                {
                    _HasMap = true;
                }
                else
                {
                    _HasMap = false;
                }
            }
            catch
            {
                _HasMap = false;
            }
            if (((cb_Left.Checked && te_Map_Left.Text.Trim().Length > 0) || (cb_Right.Checked && te_Map_Right.Text.Trim().Length > 0) || (cb_Up.Checked && te_Map_Up.Text.Trim().Length > 0) || (cb_Down.Checked && te_Map_Down.Text.Trim().Length > 0)) && _HasMap)
            {
                if (cb_Left.Checked && te_Map_Left.Text.Trim().Length > 0)
                {
                    _iLeft = Convert.ToInt16(te_Map_Left.Text.Trim());
                }
                if (cb_Right.Checked && te_Map_Right.Text.Trim().Length > 0)
                {
                    _iRight = Convert.ToInt16(te_Map_Right.Text.Trim());
                }
                if (cb_Up.Checked && te_Map_Up.Text.Trim().Length > 0)
                {
                    _iUp = Convert.ToInt16(te_Map_Up.Text.Trim());
                }
                if (cb_Down.Checked && te_Map_Down.Text.Trim().Length > 0)
                {
                    _iDown = Convert.ToInt16(te_Map_Down.Text.Trim());
                }
                _MapResize(_iLeft, _iRight, _iUp, _iDown);
                bt_Change_Map_Size_Save.Enabled = true;
            }
            else
            {
                bt_Change_Map_Size_Save.Enabled = false ;
                MessageBox.Show("没有有效的画布调整变量！");
            }
        }

        private void bt_Change_Map_Size_Save_Click(object sender, EventArgs e)
        {
            int _iLeft = 0;
            int _iUp = 0;
            try
            {
                MemoryStream ms = new MemoryStream();
                _img_b.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                string pic = Convert.ToBase64String(arr);
                if (cb_Left.Checked && te_Map_Left.Text.Trim().Length > 0)
                {
                    _iLeft = Convert.ToInt16(te_Map_Left.Text.Trim());
                }
                if (cb_Up.Checked && te_Map_Up.Text.Trim().Length > 0)
                {
                    _iUp = Convert.ToInt16(te_Map_Up.Text.Trim());
                }
                TS_Win_Class.MySqlCom("update TS_S_B_Digital_Map_Main set f_brief='" + pic + "',f_size='" + _img_b.Width.ToString() + "," + _img_b.Height.ToString() + "' where f_id=" + tl_Main.FocusedNode["f_id"].ToString());
                _img = new Bitmap(_img_b.Width, _img_b.Height);
                Graphics g = Graphics.FromImage(_img);
                g.DrawImage(_img_b, new Point(0, 0));
                MapWidth = _img.Width;
                MapHeight = _img.Height;

                string strSQL = "select * from TS_S_B_R_Digital_Map_Main_Element where f_dmmid=" + tl_Main.FocusedNode["f_id"].ToString();
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                string _str = "";
                strSQL = "";
                while (read.Read())
                {
                    strSQL+= "update TS_S_B_R_Digital_Map_Main_Element set ";
                    _str = read["f_points"].ToString();
                    if (_str.Length > 1)
                    {
                        strSQL += "f_points='" + (Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))) + _iLeft).ToString() + "," + (Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1)) + _iUp).ToString()+"'";
                    }
                    _str = read["f_pointe"].ToString();
                    if (_str.Length > 1)
                    {
                        strSQL += ",f_pointe='" + (Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))) + _iLeft).ToString() + "," + (Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1)) + _iUp).ToString()+"'";
                    }
                    strSQL += " where f_id=" + read["f_id"].ToString()+";";
                }
                read.Close();
                conn.Close();
                conn.Dispose();
                TS_Win_Class.MySqlCom(strSQL);
                Init_MapElement_List();
            }
            catch
            {
            }
            bt_Change_Map_Size_Save.Enabled = false;
        }
        
        private void bt_Map_ReLoad_Click(object sender, EventArgs e)
        {
            string _str = "";
            try
            {
                if (tl_Main.FocusedNode["f_id"].ToString().Length > 0)
                {
                    _str = TS_Win_Class.GetSQlFieldValue("select f_brief from TS_S_B_Digital_Map_Main where f_del=0 and f_id=" + tl_Main.FocusedNode["f_id"].ToString(), "f_brief");
                    if (_str.Length > 1)
                    {
                        _LoadImageByStr(_str);
                        pb_Main.Image = _img;
                        pb_Main.SizeMode = PictureBoxSizeMode.StretchImage;
                        pb_Main.Refresh();
                    }
                    else
                    {
                        _CreateImageBySize(TS_Win_Class.GetSQlFieldValue("select f_size from TS_S_B_Digital_Map_Main where f_del=0 and f_id=" + tl_Main.FocusedNode["f_id"].ToString(), "f_size"));
                        pb_Main.Image = _img;
                        pb_Main.Refresh();
                    }
                    _img_b = new Bitmap(_img.Width, _img.Height);
                    Graphics g = Graphics.FromImage(_img_b);
                    g.DrawImage(_img, new Point(0,0));

//                    g.Dispose();
                }
                else
                {
                    pb_Main.Image = null;
                    pb_Main.Refresh();
                }
            }
            catch
            {
                pb_Main.Image = null;
                pb_Main.Refresh();
            }
        }

        private void bt_Map_ReBuild_Click(object sender, EventArgs e)
        {
            _RebuidImage("ALL","",100);
        }
        #endregion
        #region//--地图移动放大缩小事件--//
        private void bt_Map_Move_Left_Click(object sender, EventArgs e)
        {
            pb_Main.Location = new Point(pb_Main.Location.X - scrollspeed, pb_Main.Location.Y);
            ScreenVariableY = 0 - pb_Main.Location.Y;
            ScreenVariableX = 0 - pb_Main.Location.X;
        }

        private void bt_Map_Move_Right_Click(object sender, EventArgs e)
        {
            pb_Main.Location = new Point(pb_Main.Location.X + scrollspeed, pb_Main.Location.Y);
            ScreenVariableY = 0 - pb_Main.Location.Y;
            ScreenVariableX = 0 - pb_Main.Location.X;
        }

        private void bt_Map_Move_Top_Click(object sender, EventArgs e)
        {
            pb_Main.Location = new Point(pb_Main.Location.X, pb_Main.Location.Y - scrollspeed);
            ScreenVariableY = 0 - pb_Main.Location.Y;
            ScreenVariableX = 0 - pb_Main.Location.X;
        }

        private void bt_Map_Move_Buttom_Click(object sender, EventArgs e)
        {
            pb_Main.Location = new Point(pb_Main.Location.X, pb_Main.Location.Y + scrollspeed);
            ScreenVariableY = 0 - pb_Main.Location.Y;
            ScreenVariableX = 0 - pb_Main.Location.X;
        }

        private void bt_Map_Zoom_In_Click(object sender, EventArgs e)
        {
            if ((pb_Main.Width / MapWidth >= 0 && pb_Main.Width / MapWidth <= 3) || (pb_Main.Width / MapWidth <= 1))
            {
                pb_Main.Height += scrollspeed;
                pb_Main.Width += scrollspeed;
                pb_Main.Location = new Point(pb_Main.Location.X - scrollspeed / 2, pb_Main.Location.Y - scrollspeed / 2);
                ScreenVariableY = 0 - pb_Main.Location.Y;
                ScreenVariableX = 0 - pb_Main.Location.X;
            }
        }

        private void bt_Map_Zoom_Out_Click(object sender, EventArgs e)
        {
            if ((pb_Main.Width / MapWidth >= 0 && pb_Main.Width / MapWidth <= 3) || (pb_Main.Width / MapWidth >= 3))
            {
                pb_Main.Height -= scrollspeed;
                pb_Main.Width -= scrollspeed;
                pb_Main.Location = new Point(pb_Main.Location.X + scrollspeed / 2, pb_Main.Location.Y + scrollspeed / 2);
                ScreenVariableY = 0 - pb_Main.Location.Y;
                ScreenVariableX = 0 - pb_Main.Location.X;
            }
        }
        private void bt_Map_Zoom_S_Click(object sender, EventArgs e)
        {
            pb_Main.Size = pc_Center.Size;
            pb_Main.Location = new Point(0, 0);
            ScreenVariableY = 0 - pb_Main.Location.Y;
            ScreenVariableX = 0 - pb_Main.Location.X;
        }
#endregion
        #region//--数字地图元素事件--//
        private void Init_MapElement_TreeList()
        {
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable("select f_id,f_name,f_size,f_type from TS_S_B_Digital_Map_Element where f_del=0", _dt);
            tl_Element.DataSource = _dt;
        }

        private void tl_Element_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            te_WorkPlace.Tag = "-1";
            try
            {
                grc_Element_Data.Tag = "E" + tl_Element.FocusedNode["f_id"].ToString();
                switch (tl_Element.FocusedNode["f_type"].ToString())
                {
                    case "0":
                        rb_E_Point.Enabled = false;
                        rb_E_Point.Checked = false;
                        te_E_X.Enabled = false;
                        bt_E_X_Add.Enabled = false;
                        bt_E_X_Des.Enabled = false;
                        te_E_Y.Enabled = false;
                        bt_E_Y_Add.Enabled = false;
                        bt_E_Y_Des.Enabled = false;

                        rb_S_Point.Enabled = true;
                        rb_S_Point.Checked = true;
                        te_S_X.Enabled = true;
                        bt_S_X_Add.Enabled = true;
                        bt_S_X_Des.Enabled = true;
                        te_S_Y.Enabled = true;
                        bt_S_Y_Add.Enabled = true;
                        bt_S_Y_Des.Enabled = true;

                        et_Element_Brief.Enabled = false;
                        break;
                    case "1":
                        rb_E_Point.Enabled = false;
                        rb_E_Point.Checked = false;
                        te_E_X.Enabled = false;
                        bt_E_X_Add.Enabled = false;
                        bt_E_X_Des.Enabled = false;
                        te_E_Y.Enabled = false;
                        bt_E_Y_Add.Enabled = false;
                        bt_E_Y_Des.Enabled = false;

                        rb_S_Point.Enabled = true;
                        rb_S_Point.Checked = true;
                        te_S_X.Enabled = true;
                        bt_S_X_Add.Enabled = true;
                        bt_S_X_Des.Enabled = true;
                        te_S_Y.Enabled = true;
                        bt_S_Y_Add.Enabled = true;
                        bt_S_Y_Des.Enabled = true;

                        et_Element_Brief.Enabled = false;
                        break;
                    case "2":
                        rb_E_Point.Enabled = false;
                        rb_E_Point.Checked = false;
                        te_E_X.Enabled = false;
                        bt_E_X_Add.Enabled = false;
                        bt_E_X_Des.Enabled = false;
                        te_E_Y.Enabled = false;
                        bt_E_Y_Add.Enabled = false;
                        bt_E_Y_Des.Enabled = false;

                        rb_S_Point.Enabled = true;
                        rb_S_Point.Checked = true;
                        te_S_X.Enabled = true;
                        bt_S_X_Add.Enabled = true;
                        bt_S_X_Des.Enabled = true;
                        te_S_Y.Enabled = true;
                        bt_S_Y_Add.Enabled = true;
                        bt_S_Y_Des.Enabled = true;

                        et_Element_Brief.Enabled = false;
                        break;
                    case "3":
                        rb_E_Point.Enabled = true;
                        rb_E_Point.Checked = true;
                        te_E_X.Enabled = true;
                        bt_E_X_Add.Enabled = true;
                        bt_E_X_Des.Enabled = true;
                        te_E_Y.Enabled = true;
                        bt_E_Y_Add.Enabled = true;
                        bt_E_Y_Des.Enabled = true;
 
                        rb_S_Point.Enabled = true;
                        rb_S_Point.Checked = true;
                        te_S_X.Enabled = true;
                        bt_S_X_Add.Enabled = true;
                        bt_S_X_Des.Enabled = true;
                        te_S_Y.Enabled = true;
                        bt_S_Y_Add.Enabled = true;
                        bt_S_Y_Des.Enabled = true;

                        et_Element_Brief.Enabled = false;
                        break;
                    case "4":
                        rb_E_Point.Enabled = false;
                        rb_E_Point.Checked = false;
                        te_E_X.Enabled = false;
                        bt_E_X_Add.Enabled = false;
                        bt_E_X_Des.Enabled = false;
                        te_E_Y.Enabled = false;
                        bt_E_Y_Add.Enabled = false;
                        bt_E_Y_Des.Enabled = false;

                        rb_S_Point.Enabled = true;
                        rb_S_Point.Checked = true;
                        te_S_X.Enabled = true;
                        bt_S_X_Add.Enabled = true;
                        bt_S_X_Des.Enabled = true;
                        te_S_Y.Enabled = true;
                        bt_S_Y_Add.Enabled = true;
                        bt_S_Y_Des.Enabled = true;

                        et_Element_Brief.Enabled = true;
                         break;
                }
            }
            catch
            {
                grc_Element_Data.Tag = "-1";
            }
        }
        #endregion
        #region//--元素参数事件--//

        private void bt_S_X_Add_Click(object sender, EventArgs e)
        {
            te_S_X.Text = (Convert.ToInt16(te_S_X.Text.Trim()) + 1).ToString();
        }

        private void bt_S_X_Des_Click(object sender, EventArgs e)
        {
            te_S_X.Text = (Convert.ToInt16(te_S_X.Text.Trim()) - 1).ToString();
        }

        private void bt_S_Y_Add_Click(object sender, EventArgs e)
        {
            te_S_Y.Text = (Convert.ToInt16(te_S_Y.Text.Trim()) + 1).ToString();
        }

        private void bt_S_Y_Des_Click(object sender, EventArgs e)
        {
            te_S_Y.Text = (Convert.ToInt16(te_S_Y.Text.Trim()) - 1).ToString();
        }

        private void bt_E_X_Add_Click(object sender, EventArgs e)
        {
            te_E_X.Text = (Convert.ToInt16(te_E_X.Text.Trim()) + 1).ToString();
        }

        private void bt_E_X_Des_Click(object sender, EventArgs e)
        {
            te_E_X.Text = (Convert.ToInt16(te_E_X.Text.Trim()) - 1).ToString();
        }

        private void bt_E_Y_Add_Click(object sender, EventArgs e)
        {
            te_E_Y.Text = (Convert.ToInt16(te_E_Y.Text.Trim()) + 1).ToString();
        }

        private void bt_E_Y_Des_Click(object sender, EventArgs e)
        {
            te_E_Y.Text = (Convert.ToInt16(te_E_Y.Text.Trim()) - 1).ToString();
        }

        private void te_WorkPlace_DoubleClick(object sender, EventArgs e)
        {
            //MessageBox.Show("选择站场工位！");

            UserCon.ucPub.Map.uc_Pub_Map_Select_ListView_ByOne_ForDigitalMap uc_Pub_Map_Select_ListView_ByOne_ForDigitalMap = new UserCon.ucPub.Map.uc_Pub_Map_Select_ListView_ByOne_ForDigitalMap();
            uc_Pub_Map_Select_ListView_ByOne_ForDigitalMap._MapID = tl_Main.FocusedNode["f_id"].ToString();
            if (uc_Pub_Map_Select_ListView_ByOne_ForDigitalMap.ShowDialog() == DialogResult.OK)
            {
                _LoadWorkPlaceName(_WorkPlaceID, _WorkPlaceName);
            }
            else
            {
                _LoadWorkPlaceName("-1", "-1");
            }
        }
        public void _LoadWorkPlaceName(string _ID,string _Name)
        {
            te_WorkPlace.Tag=_ID;
            te_WorkPlace.Text = _Name;
        }
        private void bt_Element_Preview_Click(object sender, EventArgs e)
        {
            try
            {
                if (grc_Element_Data.Tag.ToString() == "-1")
                {
                }
                if (grc_Element_Data.Tag.ToString().Substring(0,1) == "E")
                {
                    switch (tl_Element.FocusedNode["f_type"].ToString())
                    {
                        case "0":
                            if (te_S_X.Text.Trim().Length > 0 && te_S_Y.Text.Trim().Length > 0 && te_WorkPlace.Tag.ToString()!="-1")
                            {
                                _LoadInsertImageByID(tl_Element.FocusedNode["f_id"].ToString());
                                _InsertImage(Convert.ToInt16(te_S_X.Text.Trim()), Convert.ToInt16(te_S_Y.Text.Trim()));
                                bt_Element_Save.Tag = "Insert";
                                bt_Element_Save.Enabled = true;
                            }
                            break;
                        case "1":
                            if (te_S_X.Text.Trim().Length > 0 && te_S_Y.Text.Trim().Length > 0)
                            {
                                _LoadInsertImageByID(tl_Element.FocusedNode["f_id"].ToString());
                                _InsertImage(Convert.ToInt16(te_S_X.Text.Trim()), Convert.ToInt16(te_S_Y.Text.Trim()));
                                bt_Element_Save.Tag = "Insert";
                                bt_Element_Save.Enabled = true;
                            }
                            break;
                        case "2":
                            if (te_S_X.Text.Trim().Length > 0 && te_S_Y.Text.Trim().Length > 0)
                            {
                                _LoadInsertImageByID(tl_Element.FocusedNode["f_id"].ToString());
                                _InsertImage(Convert.ToInt16(te_S_X.Text.Trim()), Convert.ToInt16(te_S_Y.Text.Trim()));
                                bt_Element_Save.Tag = "Insert";
                                bt_Element_Save.Enabled = true;
                            }
                            break;
                        case "3":
                            if (te_S_X.Text.Trim().Length > 0 && te_S_Y.Text.Trim().Length > 0 && te_E_X.Text.Trim().Length > 0 && te_E_Y.Text.Trim().Length > 0)
                            {
                                _InsertLine(Convert.ToInt16(te_S_X.Text.Trim()), Convert.ToInt16(te_S_Y.Text.Trim()), Convert.ToInt16(te_E_X.Text.Trim()), Convert.ToInt16(te_E_Y.Text.Trim()));
                                bt_Element_Save.Tag = "Insert";
                                bt_Element_Save.Enabled = true;
                            }
                            break;
                        case "4":
                            if (te_S_X.Text.Trim().Length > 0 && te_S_Y.Text.Trim().Length > 0 && et_Element_Brief.Text.Trim().Length > 0)
                            {
                                _InsertWord(et_Element_Brief.Text.Trim(), Convert.ToInt16(tl_Element.FocusedNode["f_size"].ToString()), Convert.ToInt16(te_S_X.Text.Trim()), Convert.ToInt16(te_S_Y.Text.Trim()));
                                bt_Element_Save.Tag = "Insert";
                                bt_Element_Save.Enabled = true;
                            }
                            break;
                    }
                }
                if (grc_Element_Data.Tag.ToString().Substring(0, 1) == "D")
                {
                    _RebuidImage("Exclude", gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_id").ToString(), 0);
                    switch (tl_Main.FocusedNode["f_type"].ToString())
                    {
                        case "0":

                            break;
                        case "1":

                            break;
                        case "2":

                            break;
                        case "3":
                            break;
                        case "4":
                            break;
                    }
                    _RebuidHighLightElement(gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_id").ToString());
                    bt_Element_Save.Tag = "Edit";
                    bt_Element_Save.Enabled = true;
                }
            }
            catch
            {
                grc_Element_Data.Tag = "-1";
            }
        }

        private void bt_Element_Save_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            bt_Element_Save.Enabled = false;
            if (bt_Element_Save.Tag.ToString()=="Insert")
            {
                switch (tl_Element.FocusedNode["f_type"].ToString())
                {
                    case "0":
                        strSQL = "insert into TS_S_B_R_Digital_Map_Main_Element (f_dmmid,f_dmeid,f_swid,f_points,f_pointe,f_workerid) values("
                            + tl_Main.FocusedNode["f_id"].ToString() + ","
                            + tl_Element.FocusedNode["f_id"].ToString() + ","+te_WorkPlace.Tag.ToString()+",'"
                            +te_S_X.Text.Trim()+","+te_S_Y.Text.Trim()+"',',',"
                            +TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
                        break;
                    case "1":
                        strSQL = "insert into TS_S_B_R_Digital_Map_Main_Element (f_dmmid,f_dmeid,f_swid,f_points,f_pointe,f_workerid) values("
                            + tl_Main.FocusedNode["f_id"].ToString() + ","
                            + tl_Element.FocusedNode["f_id"].ToString() + ",-1,'"
                            +te_S_X.Text.Trim()+","+te_S_Y.Text.Trim()+"',',',"
                            +TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
                        break;
                    case "2":
                        strSQL = "insert into TS_S_B_R_Digital_Map_Main_Element (f_dmmid,f_dmeid,f_swid,f_points,f_pointe,f_workerid) values("
                            + tl_Main.FocusedNode["f_id"].ToString() + ","
                            + tl_Element.FocusedNode["f_id"].ToString() + ",-1,'"
                            +te_S_X.Text.Trim()+","+te_S_Y.Text.Trim()+"',',',"
                            +TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
                        break;
                    case "3":
                        strSQL = "insert into TS_S_B_R_Digital_Map_Main_Element (f_dmmid,f_dmeid,f_swid,f_points,f_pointe,f_brief,f_workerid) values("
                            + tl_Main.FocusedNode["f_id"].ToString() + ","
                            + tl_Element.FocusedNode["f_id"].ToString() + ",-1,'"
                            +te_S_X.Text.Trim()+","+te_S_Y.Text.Trim()+"','"
                            + te_E_X.Text.Trim() + "," + te_E_Y.Text.Trim() + "','"
                            + et_Element_Brief.Text.Trim() + "',"
                            +TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
                           break;
                    case "4":
                        strSQL = "insert into TS_S_B_R_Digital_Map_Main_Element (f_dmmid,f_dmeid,f_swid,f_points,f_pointe,f_brief,f_workerid) values("
                            + tl_Main.FocusedNode["f_id"].ToString() + ","
                            + tl_Element.FocusedNode["f_id"].ToString() + ",-1,'"
                            +te_S_X.Text.Trim()+","+te_S_Y.Text.Trim()+"',',','"
                            + et_Element_Brief.Text.Trim() + "',"
                            +TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
                        break;
                }
                TS_Win_Class.MySqlCom(strSQL);
            }
            if (bt_Element_Save.Tag.ToString() == "Edit")
            {
                switch (gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_type").ToString())
                {
                    case "0":
                        strSQL = "update TS_S_B_R_Digital_Map_Main_Element set f_swid=" + te_WorkPlace.Tag.ToString() + ",f_points='"
                            + te_S_X.Text.Trim() + "," + te_S_Y.Text.Trim() + "',f_workerid="
                            + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id=" + grc_Element_Data.Tag.ToString().Substring(1);
                        break;
                    case "1":
                        strSQL = "update TS_S_B_R_Digital_Map_Main_Element set f_points='"
                            + te_S_X.Text.Trim() + "," + te_S_Y.Text.Trim() + "',f_workerid="
                            + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id=" + grc_Element_Data.Tag.ToString().Substring(1);
                        break;
                    case "2":
                        strSQL = "update TS_S_B_R_Digital_Map_Main_Element set f_points='"
                            + te_S_X.Text.Trim() + "," + te_S_Y.Text.Trim() + "',f_workerid="
                            + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id=" + grc_Element_Data.Tag.ToString().Substring(1);
                        break;
                    case "3":
                        strSQL = "update TS_S_B_R_Digital_Map_Main_Element set f_points='"
                            + te_S_X.Text.Trim() + "," + te_S_Y.Text.Trim() + "',f_pointe='"
                            + te_E_X.Text.Trim() + "," + te_E_Y.Text.Trim() + "',f_workerid="
                            + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id=" + grc_Element_Data.Tag.ToString().Substring(1);
                        break;
                    case "4":
                        strSQL = "update TS_S_B_R_Digital_Map_Main_Element set f_points='"
                            + te_S_X.Text.Trim() + "," + te_S_Y.Text.Trim() + "',f_brief='"
                            + et_Element_Brief.Text.Trim() + "',f_workerid="
                            + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " where f_id=" + grc_Element_Data.Tag.ToString().Substring(1);
                        break;
                }
                TS_Win_Class.MySqlCom(strSQL);
                _RebuidImage("ALL", "", 0);
            }
            try
            {
                MemoryStream ms = new MemoryStream();
                _img_b.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                string pic = Convert.ToBase64String(arr);
                TS_Win_Class.MySqlCom("update TS_S_B_Digital_Map_Main set f_brief='" + pic + "' where f_id=" + tl_Main.FocusedNode["f_id"].ToString());
                _img = new Bitmap(_img_b.Width, _img_b.Height);
                Graphics g = Graphics.FromImage(_img);
                g.DrawImage(_img_b, new Point(0, 0));
            }
            catch
            {
            }
            bt_Element_Save.Tag = "-1";
            Init_MapElement_List();
        }
        #endregion
        #region//--数字画地图数据事件--//
        private void Init_MapMain_TreeList()
        {
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable("select f_id,f_name from TS_S_B_Digital_Map_Main where f_del=0", _dt);
            tl_Main.DataSource = _dt;
        }
        private void tl_Main_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            _img = null;
            _img_b = null;
            pb_Main.Image = null;
            pb_Main.Refresh();
            Init_MapElement_List();
        }
        private void Init_MapElement_List()
        {
            try
            {
                string strSQL = "select *,(case f_type when 0 then f_swname when 1 then f_dmename when 2 then f_dmename when 3 then f_dmename when 4 then f_dmename+':'+f_brief end) as f_name from (select f_id,f_dmmid,f_dmeid,(select f_name from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_dmename,f_swid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_S_B_R_Digital_Map_Main_Element.f_swid) as f_swname,f_points,f_pointe,f_brief,(select f_type from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_type,(select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_size from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + tl_Main.FocusedNode["f_id"].ToString() + ") as a";
                DataTable _dt = new DataTable();
                TS_Win_Class.FilledDataTable(strSQL,_dt);
                gc_Element.DataSource = _dt;
            }
            catch
            {
                gc_Element.DataSource = null;
                gv_Element.RefreshData();
            }
        }
        private void gc_Element_Click(object sender, EventArgs e)
        {
            try
            {
                te_WorkPlace.Tag = "-1";
                if (gv_Element.SelectedRowsCount > 0)
                {
                    grc_Element_Data.Tag = "D" + gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_id").ToString();
                    string _str = "";
                    string _X = "";
                    string _Y = "";
                    switch (gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_type").ToString())
                    {
                        case "0":
                            rb_E_Point.Enabled = false;
                            rb_E_Point.Checked = false;
                            te_E_X.Enabled = false;
                            bt_E_X_Add.Enabled = false;
                            bt_E_X_Des.Enabled = false;
                            te_E_Y.Enabled = false;
                            bt_E_Y_Add.Enabled = false;
                            bt_E_Y_Des.Enabled = false;

                            _str = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_points").ToString();
                            _X = _str.Substring(0, _str.IndexOf(','));
                            _Y = _str.Substring(_str.IndexOf(',') + 1);
                            rb_S_Point.Enabled = true;
                            rb_S_Point.Checked = true;
                            te_S_X.Enabled = true;
                            te_S_X.Text = _X;
                            bt_S_X_Add.Enabled = true;
                            bt_S_X_Des.Enabled = true;
                            te_S_Y.Enabled = true;
                            te_S_Y.Text = _Y;
                            bt_S_Y_Add.Enabled = true;
                            bt_S_Y_Des.Enabled = true;

                            et_Element_Brief.Enabled = false;

                            te_WorkPlace.Text = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_name").ToString();
                            te_WorkPlace.Tag = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_swid").ToString();
                            break;
                        case "1":
                            rb_E_Point.Enabled = false;
                            rb_E_Point.Checked = false;
                            te_E_X.Enabled = false;
                            bt_E_X_Add.Enabled = false;
                            bt_E_X_Des.Enabled = false;
                            te_E_Y.Enabled = false;
                            bt_E_Y_Add.Enabled = false;
                            bt_E_Y_Des.Enabled = false;

                            _str = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_points").ToString();
                            _X = _str.Substring(0, _str.IndexOf(','));
                            _Y = _str.Substring(_str.IndexOf(',') + 1);
                            rb_S_Point.Enabled = true;
                            rb_S_Point.Checked = true;
                            te_S_X.Enabled = true;
                            te_S_X.Text = _X;
                            bt_S_X_Add.Enabled = true;
                            bt_S_X_Des.Enabled = true;
                            te_S_Y.Enabled = true;
                            te_S_Y.Text = _Y;
                            bt_S_Y_Add.Enabled = true;
                            bt_S_Y_Des.Enabled = true;

                            et_Element_Brief.Enabled = false;
                            te_WorkPlace.Text = "";
                            te_WorkPlace.Tag = "-1";
                            break;
                        case "2":
                            rb_E_Point.Enabled = false;
                            rb_E_Point.Checked = false;
                            te_E_X.Enabled = false;
                            bt_E_X_Add.Enabled = false;
                            bt_E_X_Des.Enabled = false;
                            te_E_Y.Enabled = false;
                            bt_E_Y_Add.Enabled = false;
                            bt_E_Y_Des.Enabled = false;

                            _str = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_points").ToString();
                            _X = _str.Substring(0, _str.IndexOf(','));
                            _Y = _str.Substring(_str.IndexOf(',')+1);
                            rb_S_Point.Enabled = true;
                            rb_S_Point.Checked = true;
                            te_S_X.Enabled = true;
                            te_S_X.Text = _X;
                            bt_S_X_Add.Enabled = true;
                            bt_S_X_Des.Enabled = true;
                            te_S_Y.Enabled = true;
                            te_S_Y.Text = _Y;
                            bt_S_Y_Add.Enabled = true;
                            bt_S_Y_Des.Enabled = true;

                            et_Element_Brief.Enabled = false;
                            te_WorkPlace.Text = "";
                            te_WorkPlace.Tag = "-1";
                            break;
                        case "3":
                            _str = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_pointe").ToString();
                            _X = _str.Substring(0, _str.IndexOf(','));
                            _Y = _str.Substring(_str.IndexOf(',')+1);
                             rb_E_Point.Enabled = true;
                            rb_E_Point.Checked = true;
                            te_E_X.Enabled = true;
                            te_E_X.Text = _X;
                            bt_E_X_Add.Enabled = true;
                            bt_E_X_Des.Enabled = true;
                            te_E_Y.Enabled = true;
                            te_E_Y.Text = _Y;
                            bt_E_Y_Add.Enabled = true;
                            bt_E_Y_Des.Enabled = true;

                            _str = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_points").ToString();
                            _X = _str.Substring(0, _str.IndexOf(','));
                            _Y = _str.Substring(_str.IndexOf(',')+1);
                            rb_S_Point.Enabled = true;
                            rb_S_Point.Checked = true;
                            te_S_X.Enabled = true;
                            te_S_X.Text = _X;
                            bt_S_X_Add.Enabled = true;
                            bt_S_X_Des.Enabled = true;
                            te_S_Y.Enabled = true;
                            te_S_Y.Text = _Y;
                            bt_S_Y_Add.Enabled = true;
                            bt_S_Y_Des.Enabled = true;

                            et_Element_Brief.Enabled = false;
                            te_WorkPlace.Text = "";
                            te_WorkPlace.Tag = "-1";
                            break;
                        case "4":
                            rb_E_Point.Enabled = false;
                            rb_E_Point.Checked = false;
                            te_E_X.Enabled = false;
                            bt_E_X_Add.Enabled = false;
                            bt_E_X_Des.Enabled = false;
                            te_E_Y.Enabled = false;
                            bt_E_Y_Add.Enabled = false;
                            bt_E_Y_Des.Enabled = false;

                            _str = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_points").ToString();
                            _X = _str.Substring(0, _str.IndexOf(','));
                            _Y = _str.Substring(_str.IndexOf(',')+1);
                            rb_S_Point.Enabled = true;
                            rb_S_Point.Checked = true;
                            te_S_X.Enabled = true;
                            te_S_X.Text = _X;
                            bt_S_X_Add.Enabled = true;
                            bt_S_X_Des.Enabled = true;
                            te_S_Y.Enabled = true;
                            te_S_Y.Text = _Y;
                            bt_S_Y_Add.Enabled = true;
                            bt_S_Y_Des.Enabled = true;

                            et_Element_Brief.Enabled = true;
                            et_Element_Brief.Text = gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_brief").ToString();
                            te_WorkPlace.Text = "";
                            te_WorkPlace.Tag = "-1";
                            break;
                    }
                    _HighLightElement(gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_id").ToString());
                }
            }
            catch
            {
                te_WorkPlace.Tag = "-1";
            }
        }
        #endregion
        #region//--图片显示区事件--//
        private void pb_Main_MouseClick(object sender, MouseEventArgs e)
        {
            if (rb_S_Point.Checked)
            {
                te_S_X.Text = (e.X * MapWidth / pb_Main.Width).ToString();
                te_S_Y.Text = (e.Y * MapHeight / pb_Main.Height).ToString();
            }
            if (rb_E_Point.Checked)
            {
                te_E_X.Text = (e.X * MapWidth / pb_Main.Width).ToString();
                te_E_Y.Text = (e.Y * MapHeight / pb_Main.Height).ToString();
            }
            //_InsertWord(_img_b.Width.ToString() + ',' + _img_b.Height.ToString(), 12, e.X * MapWidth / pb_Main.Width, e.Y * MapHeight / pb_Main.Height);
        }
        #endregion
        #region//--图像处理函数--//
        private Image _LoadImageByStr(string _str)
        {
            try
            {
                if (_str.Length > 100)
                {
                    byte[] bt = Convert.FromBase64String(_str);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                    _img = new Bitmap(stream);
                    MapWidth = _img.Width;
                    MapHeight = _img.Height;
                }
            }
            catch
            {
            }
            return _img;
        }
        private Image _CreateImageBySize(string _str)
        {
            try
            {
                if (_str.IndexOf(',') > 0)
                {
                    _img = new Bitmap(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1)));
                    Graphics g = Graphics.FromImage(_img);
                    //清除画布,背景设置为白色
                    g.Clear(System.Drawing.Color.Black);
                    MapWidth = _img.Width;
                    MapHeight = _img.Height;
                }
            }
            catch
            {
                _img = null;
            }
            return _img;
        }
        private Image _CreateImageBySize(string _X, string _Y)
        {
            try
            {
                _img = new Bitmap(Convert.ToInt16(_X), Convert.ToInt16(_Y));
                Graphics g = Graphics.FromImage(_img);
                //清除画布,背景设置为白色
                g.Clear(System.Drawing.Color.Black);
            }
            catch
            {
                _img = null;
            }
            return _img;
        }
        private Image _CreateImageBySize(int _X, int _Y)
        {
            try
            {
                _img = new Bitmap(_X, _Y);
                Graphics g = Graphics.FromImage(_img);
                //清除画布,背景设置为白色
                g.Clear(System.Drawing.Color.Black);
            }
            catch
            {
                _img = null;
            }
            return _img;
        }
        private void _InsertWord(string _Word,int _FontSize,int _X,int _Y)
        {
//            _img_b = new Bitmap(_img.Width, _img.Height);
            Graphics g = Graphics.FromImage(new Bitmap(1, 1));
            Font font = new Font("宋体", _FontSize);
            //SizeF sizeF = g.MeasureString(_Word, font); //测量出字体的高度和宽度  
            Brush brush; //笔刷，颜色  
            //Pen _pen;
            //_pen = Pens.Red;
            brush = Brushes.White;
            PointF pf_word = new PointF(_X, _Y);
            //PointF pf_word_p = new PointF(_X, _Y);
            //PointF pf = new PointF(_X, _Y);
            g = Graphics.FromImage(_img_b);
            //g.DrawLine(_pen, pf_word_p, pf);
            //g.FillRectangle(Brushes.DodgerBlue, pf_word.X, pf_word.Y, sizeF.Width, sizeF.Height);
            //g.FillEllipse(Brushes.Red, _X - 5, _Y - 5, 10, 10);
            g.DrawImage(_img, new Point(0, 0));
            g.DrawString(_Word, font, brush, pf_word);
            pb_Main.Image = _img_b;
        }
        private void _MapResize(int _Left, int _Right,int _Up,int _Down)
        {
            _img_b = new Bitmap(_img.Width+_Left+_Right, _img.Height+_Up+_Down);
            Graphics g = Graphics.FromImage(_img_b);
            g.Clear(Color.Black);
            g.DrawImage(_img, new Point(_Left, _Up));
            pb_Main.Image = _img_b;
        }
        private void _InsertLine(int _SX, int _SY,int _EX, int _EY)
        {
            Graphics g = Graphics.FromImage(new Bitmap(1, 1));
            Pen _pen;
            _pen = new Pen(Color.White,_Pen_Wid);
            //_pen.Width = (float)0.4;
            g = Graphics.FromImage(_img_b);
            g.DrawImage(_img, new Point(0, 0));
            g.DrawLine(_pen, new Point(_SX,_SY),new Point(_EX,_EY));
            pb_Main.Image = _img_b;
        }
        private Image _LoadInsertImageByID(string _ID)
        {
            string _str = TS_Win_Class.GetSQlFieldValue("select f_brief from TS_S_B_Digital_Map_Element where f_id="+_ID, "f_brief");
            try
            {
                if (_str.Length > 100)
                {
                    byte[] bt = Convert.FromBase64String(_str);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                    _img_i = new Bitmap(stream);
                }
            }
            catch
            {
            }
            return _img;
        }
        private void _InsertImage(int _SX, int _SY)
        {
            Graphics g = Graphics.FromImage(new Bitmap(1, 1));
            g = Graphics.FromImage(_img_b);
            g.DrawImage(_img, new Point(0, 0));
            g.DrawImage(_img_i, new Point(_SX, _SY));
            pb_Main.Image = _img_b;
        }
        private void _HighLightElement(string _ID)
        {
            string _str = "";
            string _stre = "";
            string strSQL = "select *,(case f_type when 0 then f_swname when 1 then f_dmename when 2 then f_dmename when 3 then f_dmename when 4 then f_dmename+':'+f_brief end) as f_name from (select f_id,f_dmmid,f_dmeid,(select f_name from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_dmename,f_swid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_S_B_R_Digital_Map_Main_Element.f_swid) as f_swname,f_points,f_pointe,f_brief,(select f_type from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_type,(select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_size from TS_S_B_R_Digital_Map_Main_Element where f_id=" + _ID + ") as a";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            _img_h = new Bitmap(_img.Width, _img.Height);
            Graphics g = Graphics.FromImage(_img_h);
            g.DrawImage(_img,new Point(0,0));
            Font font = new Font("宋体", 6);
            Brush brush; //笔刷，颜色  
            Pen _pen;
            _pen = new Pen(Color.Red, _Pen_Wid);
            brush = Brushes.Red;
            while (read.Read())
            {
                switch (read["f_type"].ToString())
                {
                    case "0":
                        _str = read["f_points"].ToString();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        font = new Font("宋体", 16);
                        g.DrawString(read["f_name"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))-40));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    case "1":
                        _str = read["f_points"].ToString();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        font = new Font("宋体", 16);
                        g.DrawString(read["f_name"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))-40));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    case "2":
                        _str = read["f_points"].ToString();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        font = new Font("宋体", 16);
                        g.DrawString(read["f_name"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))-40));
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    case "3":
                        _str = read["f_points"].ToString();
                        _stre = read["f_pointe"].ToString();
                        g.DrawLine(_pen, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))), new Point(Convert.ToInt16(_stre.Substring(0, _stre.IndexOf(','))), Convert.ToInt16(_stre.Substring(_stre.IndexOf(',') + 1))));
                        font = new Font("宋体", 16);
                        g.DrawString(read["f_name"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))-40));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    case "4":
                        _str = read["f_points"].ToString();
                        font = new Font("宋体", Convert.ToInt16(read["f_size"].ToString()));
                        g.DrawString(read["f_brief"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    default:
                        break;
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();
        }
        private void _RebuidImage(string _type, string _ExcludeID, int _iSleepTime)
        {
            string strSQL = "select f_size from TS_S_B_Digital_Map_Main where f_id=" + tl_Main.FocusedNode["f_id"].ToString();
            string _str = TS_Win_Class.GetSQlFieldValue(strSQL, "f_size");
            string _stre = TS_Win_Class.GetSQlFieldValue(strSQL, "f_size");
            _CreateImageBySize(_str);
            pb_Main.Image = _img;
            pb_Main.Refresh();
            if (_type == "ALL")
            {
                strSQL = "select *,(case f_type when 0 then f_swname when 1 then f_dmename when 2 then f_dmename when 3 then f_dmename when 4 then f_dmename+':'+f_brief end) as f_name from (select f_id,f_dmmid,f_dmeid,(select f_name from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_dmename,f_swid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_S_B_R_Digital_Map_Main_Element.f_swid) as f_swname,f_points,f_pointe,f_brief,(select f_type from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_type,(select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_size from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + tl_Main.FocusedNode["f_id"].ToString() + ") as a";
            }
            if (_type == "Exclude")
            {
                strSQL = "select *,(case f_type when 0 then f_swname when 1 then f_dmename when 2 then f_dmename when 3 then f_dmename when 4 then f_dmename+':'+f_brief end) as f_name from (select f_id,f_dmmid,f_dmeid,(select f_name from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_dmename,f_swid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_S_B_R_Digital_Map_Main_Element.f_swid) as f_swname,f_points,f_pointe,f_brief,(select f_type from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_type,(select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_size from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + tl_Main.FocusedNode["f_id"].ToString() + " and f_id<>" + _ExcludeID + ") as a";
            }
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            _img_b = new Bitmap(_img.Width, _img.Height);
            Graphics g = Graphics.FromImage(_img_b);
            g.Clear(Color.Black);
            Font font = new Font("宋体", 6);
            Brush brush; //笔刷，颜色  
            Pen _pen;
            _pen = new Pen(Color.White, _Pen_Wid);
            brush = Brushes.White;
            PointF pf_word = new PointF(0, 0);
            while (read.Read())
            {
                switch (read["f_type"].ToString())
                {
                    case "0":
                        _str = read["f_points"].ToString();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        break;
                    case "1":
                        _str = read["f_points"].ToString();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        break;
                    case "2":
                        _str = read["f_points"].ToString();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        break;
                    case "3":
                        _str = read["f_points"].ToString();
                        _stre = read["f_pointe"].ToString();
                        g.DrawLine(_pen, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))), new Point(Convert.ToInt16(_stre.Substring(0, _stre.IndexOf(','))), Convert.ToInt16(_stre.Substring(_stre.IndexOf(',') + 1))));
                        break;
                    case "4":
                        _str = read["f_points"].ToString();
                        font = new Font("宋体", Convert.ToInt16(read["f_size"].ToString()));
                        g.DrawString(read["f_brief"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        break;
                    default:
                        break;
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            pb_Main.Image = _img_b;
            pb_Main.Refresh();
        }
        private void _RebuidHighLightElement(string _ID)
        {
            string _str = "";
            string _stre = "";
            string strSQL = "select *,(case f_type when 0 then f_swname when 1 then f_dmename when 2 then f_dmename when 3 then f_dmename when 4 then f_dmename+':'+f_brief end) as f_name from (select f_id,f_dmmid,f_dmeid,(select f_name from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_dmename,f_swid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_S_B_R_Digital_Map_Main_Element.f_swid) as f_swname,f_points,f_pointe,f_brief,(select f_type from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_type,(select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) as f_size from TS_S_B_R_Digital_Map_Main_Element where f_id=" + _ID + ") as a";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            _img_h = new Bitmap(_img.Width, _img.Height);
            Graphics g = Graphics.FromImage(_img_h);
            g.DrawImage(_img_b,new Point(0,0));
            Font font = new Font("宋体", 6);
            Brush brush; //笔刷，颜色  
            Pen _pen;
            _pen = new Pen(Color.Red, _Pen_Wid);
            brush = Brushes.Red;
            while (read.Read())
            {
                switch (read["f_type"].ToString())
                {
                    case "0":
                        _str = te_S_X.Text.Trim()+","+te_S_Y.Text.Trim();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        font = new Font("宋体", 16);
                        g.DrawString(read["f_name"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))-40));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    case "1":
                        _str = te_S_X.Text.Trim()+","+te_S_Y.Text.Trim();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        font = new Font("宋体", 16);
                        g.DrawString(read["f_name"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))-40));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    case "2":
                        _str = te_S_X.Text.Trim()+","+te_S_Y.Text.Trim();
                        _LoadInsertImageByID(read["f_dmeid"].ToString());
                        font = new Font("宋体", 16);
                        g.DrawString(read["f_name"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))-40));
                        g.DrawImage(_img_i, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    case "3":
                        _str = te_S_X.Text.Trim()+","+te_S_Y.Text.Trim();
                        _stre = te_E_X.Text.Trim() + "," + te_E_Y.Text.Trim();
                        g.DrawLine(_pen, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))), new Point(Convert.ToInt16(_stre.Substring(0, _stre.IndexOf(','))), Convert.ToInt16(_stre.Substring(_stre.IndexOf(',') + 1))));
                        font = new Font("宋体", 16);
                        g.DrawString(read["f_name"].ToString(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))-40));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    case "4":
                        _str = te_S_X.Text.Trim()+","+te_S_Y.Text.Trim();
                        font = new Font("宋体", Convert.ToInt16(read["f_size"].ToString()));
                        g.DrawString(et_Element_Brief.Text.Trim(), font, brush, new Point(Convert.ToInt16(_str.Substring(0, _str.IndexOf(','))), Convert.ToInt16(_str.Substring(_str.IndexOf(',') + 1))));
                        pb_Main.Image = _img_h;
                        pb_Main.Refresh();
                        Thread.Sleep(100);
                        break;
                    default:
                        break;
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();
        }
#endregion

        private void cms_MapElement_Opening(object sender, CancelEventArgs e)
        {
            if (gv_Element.SelectedRowsCount > 0)
            {
                cms_MapElement_Del.Enabled = true;
            }
            else
            {
                cms_MapElement_Del.Enabled = false;
            }
        }

        private void cms_MapElement_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_S_B_R_Digital_Map_Main_Element set f_del=1 where f_id=" + gv_Element.GetRowCellValue(gv_Element.GetSelectedRows()[0], "f_id").ToString();
            TS_Win_Class.MySqlCom(strSQL);
            _RebuidImage("ALL", "", 0);
            try
            {
                MemoryStream ms = new MemoryStream();
                _img_b.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                string pic = Convert.ToBase64String(arr);
                TS_Win_Class.MySqlCom("update TS_S_B_Digital_Map_Main set f_brief='" + pic + "' where f_id=" + tl_Main.FocusedNode["f_id"].ToString());
                _img = new Bitmap(_img_b.Width, _img_b.Height);
                Graphics g = Graphics.FromImage(_img);
                g.DrawImage(_img_b, new Point(0, 0));
            }
            catch
            {
            }
            bt_Element_Save.Tag = "-1";
            bt_Element_Save.Enabled = false;
            Init_MapElement_List();
        }
    }
}